/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ove.openmath.mathematica;

/**
 *
 * @author Mr.Sam
 */
import javax.swing.*;
import java.awt.*;
import java.beans.*;
import java.util.*;
import java.io.*;
import fr.ove.openmath.mathematica.*;
import fr.ove.openmath.mathematica.events.*;
import fr.ove.utils.*;

/**
* A work sheet file chooser.
*/
public class WorkSheetFileChooser extends FileChooser implements VetoableChangeListener {
    /**
    * The constructor.
    */
    public WorkSheetFileChooser() {
        this(null);
    }
    
    /**
    * The constructor.
    */
    public WorkSheetFileChooser(Component parent) {
        super(parent);
        setFileFilter(new GenericFileFilter("Work Sheet", "wks"));
    }
    
    /**
    * Indicates if we have vetoed the close operation (user pressed cancel button).
    */
    private boolean closedVetoed = false;
    
    // ####  interface VetoableChangeListener implementation
    
    /**
    *
    */
    public void vetoableChange(PropertyChangeEvent pce) throws PropertyVetoException {
        String propertyName = pce.getPropertyName();
        if (propertyName.equals(JInternalFrame.IS_CLOSED_PROPERTY)) {
            if (!closedVetoed) {
                // We checked if we vetoed the closed operation.
                // If not, do the save process.
                MyJInternalFrame jif = (MyJInternalFrame) pce.getSource();
                if (jif.isSaveNeeded()) {
                    
                    int reply = JOptionPane.showConfirmDialog(getCompReference(), 
	    	                                                    "Do you want to save the work sheet", 
	    	                                                    "Save Confirmation" , 
	    	                                                    JOptionPane.YES_NO_CANCEL_OPTION, 
	    	                                                    JOptionPane.QUESTION_MESSAGE);
			        // If the confirmation was affirmative, handle exiting.
			        if (reply == JOptionPane.YES_OPTION) {
                        File file;
                        String title = jif.getTitle();
                        if (title.startsWith("Untitled")) {
                            int state = showSave();
                            file = getSelectedFile();
                            if ((file != null) && (state == JFileChooser.APPROVE_OPTION)) {
                                jif.save(file);
                            }
                            else {
                                closedVetoed = true;  // Set we veto the close operation.
                                throw new PropertyVetoException("Save Cancelled", pce);
                            }
                        }
                        else {
                            file = new File(title);
                            jif.save(file);
                        }
                    }
                    else if (reply == JOptionPane.CANCEL_OPTION) {
                        closedVetoed = true;  // Set we veto the close operation.
                        throw new PropertyVetoException("Save Cancelled", pce);
                    }
                }
            }
            else
                closedVetoed = false;
        }
    }
}
